import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { UtilService } from "../../my-service/util.service";
import {RollService} from "../../my-service/roll.service";
import {Router} from "@angular/router";

@Component({
  selector: 'start-root',
  templateUrl: './start.component.html',
  styleUrls: ['./start.component.css'],
})
export class StartComponent implements OnInit {
  readImgUrl:string = null;
  rollIndex = {
    stuGuid:null,
    joinIsNull:false, //是否有加入的班级
    isLoading:true,//是否显示加载画面
    totalJoinRolls:[],//学生总共加入的班级
  };
  constructor(
    private message: NzMessageService,
    private utilServe:UtilService,
    private rollServe:RollService,
    private router:Router
  ) {
    this.readImgUrl =this.utilServe.readImgUrl;
  }
  /*获取所有加入班级的Guid列表*/
  getJoinClassGuidList(stuGuid) {
    this.rollServe.getJoinClass(stuGuid).then(res=>{
      if(res.status == 1){
        let classList = '';
        res.data.forEach((item,index)=>{
          if(index!==res.data.length-1){
            classList+='"'+item.FK_ClassGuid+'"'+',';
          }else {
            classList+='"'+item.FK_ClassGuid+'"'
          }
        });
        if(!classList){
          this.rollIndex.joinIsNull = true;
          this.rollIndex.isLoading =false;
        }else {
          this.getClassList(classList);
        }
      }else {
        console.log(res);
        this.message.error('服务器内部错误，请刷新重试');
      }
    })
  }
  //获取班级加入列表
  getClassList(classGuidList) {
    this.rollIndex.totalJoinRolls = [];
    //每次进入之前先清空数据，避免出现数据重复的情况
    this.rollServe.getClassList(classGuidList).then(res=>{
      if(res.status == 1){
        this.rollIndex.isLoading = false;
        res.data.forEach(item=>{
            this.rollIndex.totalJoinRolls.push(item);
        });
      }else {
        console.log(res);
        this.message.error('服务器内部错误，请刷新重试');
      }
    })
  }
  ngOnInit() {
    //获取用户Guid
    this.utilServe.getUserInfo().then(res=>{
      if(res.status == 1){
           this.rollIndex.stuGuid = res.userGuid;
          this.getJoinClassGuidList(this.rollIndex.stuGuid);
      }
    });
  }
  goClass(classGuid){
    this.router.navigate(['class',{classGuid}]);
  }
}
